package com.becca.cake.core.map.baidu.map;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.becca.cake.core.constant.CommonConstants;
import com.becca.cake.util.DataEncode;

/**
 * 百度地图通过地址湖区经纬度
 * @copyright  尊星(上海)信息科技有限公司
 * @author hejinguo
 * @date 2016年7月7日 下午10:29:41
 */
public class BaiduLatitudeHelp {
    /**
     * 百度地图通过地址获取经纬度
     * @param address
     * @return
     */
    public static Map<String, String> getGeocoderLatitude(String address) {
        //返回对象
        Map<String, String> map = null;
        BufferedReader in = null;
        try {
            //将地址转换成utf-8的16进制  
            address = URLEncoder.encode(address, DataEncode.UTF8);
            String baiduGeocoderUrl = CommonConstants.BAIDU_MAP_GEOCODING_URL
                .replace("#address#", address)
                .replace("#mapApiKey#", CommonConstants.BAIDU_MAP_APIKEY)
                .replace("#city#", CommonConstants.BAIDU_MAP_DEFAULT_CITY);
            URL tirc = new URL(baiduGeocoderUrl);
            in = new BufferedReader(new InputStreamReader(tirc.openStream(), DataEncode.UTF8));
            String res;
            StringBuilder sb = new StringBuilder("");
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            String str = sb.toString();
            JSONObject jsonObject = JSONObject.parseObject(str);
            Integer status = jsonObject.getInteger("status");
            if (status.intValue() == 0) {//成功状态
                JSONObject resultJson = JSONObject.parseObject(jsonObject.getString("result"));
                //                Integer precise = resultJson.getInteger("precise");//是否为精确查找
                JSONObject locationJson = JSONObject.parseObject(resultJson.getString("location"));
                map = new HashMap<String, String>();
                String lng = locationJson.getString("lng");
                String lat = locationJson.getString("lat");
                map.put("lng", lng);
                map.put("lat", lat);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * 百度地图通过经纬度获取地址
     * @param location
     * @return
     */
    public static Map<String, String> getLatitudeGeocoder(String location) {
        //返回对象
        Map<String, String> map = null;
        BufferedReader in = null;
        try {
            //封装请求地址参数
            String baiduGeocoderUrl = CommonConstants.BAIDU_MAP_COORDINATE_GEOCODING_URL.replace(
                "#location#", location).replace("#mapApiKey#", CommonConstants.BAIDU_MAP_APIKEY);
            URL tirc = new URL(baiduGeocoderUrl);
            in = new BufferedReader(new InputStreamReader(tirc.openStream(), DataEncode.UTF8));
            String res;
            StringBuilder sb = new StringBuilder("");
            while ((res = in.readLine()) != null) {
                sb.append(res.trim());
            }
            String str = sb.toString();
            JSONObject jsonObject = JSONObject.parseObject(str);
            Integer status = jsonObject.getInteger("status");
            if (status.intValue() == 0) {//成功状态
                JSONObject resultJson = JSONObject.parseObject(jsonObject.getString("result"));
                JSONObject addressComponentJson = JSONObject.parseObject(resultJson
                    .getString("addressComponent"));//获取地址信息
                map = new HashMap<String, String>();
                String district = addressComponentJson.getString("district");
                map.put("district", district);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}
